package one.stand.controller.index;

import com.github.pagehelper.PageHelper;
import com.jmp.api.dto.*;
import com.jmp.api.util.LabelUtil;
import com.jmp.base.LabelApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.advice.ResultEnum;
import one.stand.dto.*;
import one.stand.enums.SeeRecordEnums;
import one.stand.mapper.GoodsApiCrmAppMapper;
import one.stand.model.Goods;
import one.stand.model.ResultModel;
import one.stand.service.CompanyBaseService;
import one.stand.service.GoodsBaseService;
import one.stand.service.GoodsPhotoService;
import one.stand.service.RecordPublicService;
import one.stand.service.sys.SysDictValueService;
import one.stand.util.AssertUtil;
import one.stand.util.OneListUtil;
import one.stand.util.OneStringUtils;
import one.stand.util.SqlUtil;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author cjr
 */
@Api(description = "搜索")
@RestController
@RequestMapping("search")
public class SearchController {
    @Autowired
    private SysDictValueService sysDictValueService;
    @Autowired
    private GoodsBaseService goodsBaseService;
    @Autowired
    private RecordPublicService recordPublicService;
    @Autowired
    private CompanyBaseService companyBaseService;
    @Autowired
    private GoodsPhotoService goodsPhotoService;


    @Autowired
    private GoodsApiCrmAppMapper goodsApiCrmAppMapper;

    @ApiOperation(value = "1搜索关键词")
    @GetMapping("keyword")
    public ResultModel<SearchKeywordResponse> keyword() {

        DictDto dictDto = sysDictValueService.get2(SysDictValueService.Type.SEARCH, SysDictValueService.TypeName.SEARCH_KEYWORD);
        List<String> keywords = Objects.nonNull(dictDto) ? dictDto.getValueList() : null;

        SearchKeywordResponse response = SearchKeywordResponse.builder()
                .keywords(keywords)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation("2商品列表")
    @GetMapping("/goods")
    public ResultModel<SearchGoodsResponse> goods(@RequestAttribute SearchGoodsRequest request) {

        AssertUtil.nonNull(request.getKeyword(), ResultEnum.PARAM_CHECK, "请输入搜索词");

        PageResponse2<Integer> goodsIds;
        String nameLike = SqlUtil.like(request.getKeyword());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<Integer> goodsIdList = goodsApiCrmAppMapper.selectSearch(nameLike, null, null);
        goodsIds = ConvertUtils.page2(goodsIdList, id -> id);

        PageResponse2<SearchGoodsResponse.Goods> goodsList = ConvertUtils.page2(goodsIds, goodsId -> {
            List<GoodsTypeDto> platformTypes = goodsBaseService.getTypes(goodsId);
            if (OneListUtil.isEmpty(platformTypes)) return null;

            Goods goods = goodsBaseService.get(goodsId);
            Long popularityNum = recordPublicService.getPopularity(SeeRecordEnums.PRODUCT.getCode(), goods.getGoodsId());
            boolean hasVr = StringUtils.isNoneBlank(goods.getQjVrUrl());
            boolean hasAr = goodsBaseService.hasAr(goodsId);
            boolean hasVideo = StringUtils.isNoneBlank(goods.getVideoUrl());
            boolean hasZkgx = companyBaseService.hasZkgx(goods.getCompanyId());
            Company2Dto company = companyBaseService.get2(goods.getCompanyId());

            return SearchGoodsResponse.Goods.builder()
                    .goodsId(goodsId)
                    .photo(OneStringUtils.photoResize(goodsPhotoService.getPath(goods.getGoodsId())))
                    .goodsName(goods.getGoodsName())
                    .companyName(Objects.nonNull(company) ? company.getCompanyName() : null)
                    .labels(LabelUtil.getLabels(hasVr, hasAr, hasVideo, hasZkgx))
                    .popularityNum(popularityNum)
                    .build();
        });

        SearchGoodsResponse response = SearchGoodsResponse.builder()
                .goodsPage(goodsList)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation("3企业列表")
    @GetMapping("/company")
    public ResultModel<SearchCompanyResponse> goods(@RequestAttribute SearchCompanyRequest request) {

        AssertUtil.nonNull(request.getKeyword(), ResultEnum.PARAM_CHECK, "请输入搜索词");

        String nameLike = SqlUtil.like(request.getKeyword());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<Integer> companyIds = goodsApiCrmAppMapper.selectCompany(nameLike, LabelApi.LABEL_TYPE_COMPANY, CompanyBaseService.LABEL_SUPPLIER);

        PageResponse2<SearchCompanyResponse.Company> companyPage = ConvertUtils.page2(companyIds, companyId -> {
            Company2Dto company = companyBaseService.get2(companyId);
            boolean hasZkgx = companyBaseService.hasZkgx(company.getCompanyId());
            List<Integer> goodsIdList = goodsApiCrmAppMapper.selectCompanyGoods(companyId, nameLike);

            return SearchCompanyResponse.Company.builder()
                    .companyId(company.getCompanyId())
                    .companyName(company.getCompanyName())
                    .companyLogo(company.getCompanyLogo())
                    .companyLevel(company.countCompanyLevelView())
                    .hasZkgx(BooleanUtils.toInteger(hasZkgx))
                    .goodsList(ConvertUtils.list(goodsIdList, goodsId -> SearchCompanyResponse.Goods.builder()
                            .goodsId(goodsId)
                            .photo(OneStringUtils.photoResize(goodsPhotoService.getPath(goodsId)))
                            .build()))
                    .build();
        });

        SearchCompanyResponse response = SearchCompanyResponse.builder()
                .companyPage(companyPage)
                .build();
        return ResultModel.success(response);
    }
}
